<?php

namespace addons\xpframework\library\xcore\xcore\utils;

use Exception;
use think\Url;
use think\Config;
use think\Request;
use think\Response;
use think\response\Redirect;
use think\View as ViewTemplate;
use think\exception\HttpResponseException;

/**
 * Http 返回的工具类
 */
class XcResponseUtils
{
    public static function error($msg = '', $url = null, $data = '', $wait = 999, array $header = [])
    {
        if (is_null($url)) {
            $url = Request::instance()->isAjax() ? '' : 'javascript:history.back(-1);';
        } elseif ('' !== $url && !strpos($url, '://') && 0 !== strpos($url, '/')) {
            $url = Url::build($url);
        }

        $type = static::getResponseType();
        $result = [
            'code' => 0,
            'msg'  => $msg,
            'data' => $data,
            'url'  => $url,
            'wait' => $wait,
        ];

        if ('html' == strtolower($type)) {
            $template = Config::get('template');
            $view = Config::get('view_replace_str');

            $result = ViewTemplate::instance($template, $view)
                ->fetch(Config::get('dispatch_error_tmpl'), $result);
        }

        if (XcConfigUtils::isDebug()) {
            throw new Exception($msg);
        } else {
            $response = Response::create($result, $type)->header($header);
            throw new HttpResponseException($response);
        }
    }

    private static function getResponseType()
    {
        return Request::instance()->isAjax()
            ? Config::get('default_ajax_return')
            : Config::get('default_return_type');
    }

    /**
     * Copy from traits\controller\Jump.php#success function
     */
    public static function success($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
    {
        if (is_null($url) && ! is_null(Request::instance()->server('HTTP_REFERER'))) {
            $url = Request::instance()->server('HTTP_REFERER');
        } elseif ('' !== $url && ! strpos($url, '://') && 0 !== strpos($url, '/')) {
            $url = Url::build($url);
        }

        $type = Request::instance()->isAjax() ? Config::get('default_ajax_return') : Config::get('default_return_type');
        $result = [
            'code' => 1,
            'msg' => $msg,
            'data' => $data,
            'url' => $url,
            'wait' => $wait
        ];

        if ('html' == strtolower($type)) {
            $template = Config::get('template');
            $view = Config::get('view_replace_str');

            $result = ViewTemplate::instance($template, $view)->fetch(Config::get('dispatch_success_tmpl'), $result);
        }

        $response = Response::create($result, $type)->header($header);

        throw new HttpResponseException($response);
    }


    public static function redirect($url, $params = [], $code = 302, $with = [])
    {
        if (is_integer($params)) {
            $code   = $params;
            $params = [];
        }

        $response = new Redirect($url);
        $response->code($code)->params($params)->with($with);

        throw new HttpResponseException($response);
    }

    /**
     * @usage 
     *  return selectpage($list);
        <input id="c-user_id" data-rule="required" name="row[user_id]" value="" 
            type="text" class="form-control selectpage"
            data-source="user/user/index" data-field="nickname"
            data-params='{"custom[nickname]":"{$row.nickname|htmlentities}"}' >
     */
    public static function selectpage($list, $total = null)
    {
        $total = ($total) ? $total : count($list);
        return json(['list' => $list, 'total' => $total]);
    }
}
